package fruitsManage;

import java.util.ArrayList;
import java.util.Scanner;

public class Fruits {

	static ArrayList<FruitsModel> fruits = new ArrayList<FruitsModel>();// 水果集合

	static Scanner scanner = new Scanner(System.in);

	static double salesTotal;// 累计销售金额
	static double bidTotal;// 累计进货金额

	/**
	 * 水果管理列表
	 */
	public void fruitsList() {
		FruitsModel fruitsModel = new FruitsModel();
		Sales sales = new Sales();
		while (true) {
			System.out.println();
			System.out.println("=========请选择========");
			System.out.println("=========1-查看所有====");
			System.out.println("=========2-查询========");
			System.out.println("=========3-修改水果信息==");
			System.out.println("=========4-销售========");
			System.out.println("=========5-退出========");

			int key = 5;
			try {
				key = Integer.parseInt(scanner.nextLine());
			} catch (Exception e) {
				System.out.println("输入有误！");
				return;
			}
			if (key == 5)
				break;

			switch (key) {
			case 1:
				fruitsModel.selectAll();
				break;
			case 2:
				System.out.print("请输入要查询的水果名：");
				String name = scanner.nextLine();
				fruitsModel.selectByName(name);
				break;
			case 3:
				update();
				break;
			case 4:
				sales.addSale();
				break;
			default:
				break;
			}

			System.out.println();
			System.out.println("点击回车继续...");
			scanner.nextLine();
		}
	}

	// 修改水果信息
	public void update() {
		FruitsModel fruitsModel = new FruitsModel();

		System.out.print("请输入水果名：");
		String name = scanner.nextLine();

		fruitsModel.selectByName(name);// 将要修改的水果信息展示出来
		int id = fruitsModel.searchId(name);
		// 判断是否存在这个水果
		if (id == -1) {
			System.out.println("未找到这个水果！");
			return;
		}

		System.out.println();
		System.out.println("========修改信息=======");
		System.out.print("水果名：");
		String newName = scanner.nextLine();

		System.out.print("库存：");
		double newStock = fruits.get(id).getStock();
		try {
			newStock = Integer.parseInt(scanner.nextLine());
		} catch (Exception e) {
			System.out.println("库存输入有误！");
			return;
		}

		System.out.print("售价：");
		double newPrice = fruits.get(id).getPrice();
		try {
			newPrice = Double.parseDouble(scanner.nextLine());

		} catch (Exception e) {
			System.out.println("售价输入有误！");
		}

		System.out.print("进价不可修改！");

		System.out.println("是否确认修改？（1-确认/2-取消）");

		int i = 2;
		try {
			i = Integer.parseInt(scanner.nextLine());
		} catch (Exception e) {

		}
		if (i == 2) {
			System.out.println("修改失败！");
			return;
		}

		fruitsModel.updateFruit(id, newName, newStock, newPrice);

		System.out.println("====================");

		fruitsModel.selectByName(newName);// 将修改后的水果信息显示出来

		System.out.println("修改成功！");

	}


	/**
	 * 初始化水果
	 */
	public void init() {

		FruitsModel fruitsModel1 = new FruitsModel();
		fruitsModel1.setName("苹果");
		fruitsModel1.setStock(521);
		fruitsModel1.setPrice(21.0);
		fruitsModel1.setBid(5.21);
		fruits.add(fruitsModel1);

		FruitsModel fruitsModel2 = new FruitsModel();
		fruitsModel2.setName("香蕉");
		fruitsModel2.setStock(432);
		fruitsModel2.setPrice(4.6);
		fruitsModel2.setBid(2.4);
		fruits.add(fruitsModel2);

		FruitsModel fruitsModel3 = new FruitsModel();
		fruitsModel3.setName("猕猴桃");
		fruitsModel3.setStock(1000);
		fruitsModel3.setPrice(2);
		fruitsModel3.setBid(0.5);

		// 初始化进货金额
		Fruits.bidTotal = fruitsModel1.getBid() * fruitsModel1.getStock()
				+ fruitsModel2.getBid() * fruitsModel2.getStock() + fruitsModel3.getBid() * fruitsModel3.getStock();

		fruits.add(fruitsModel3);

	}

}
